import lombok.Builder;
import lombok.Data;

import java.util.Vector;


public class EnemyTank extends Tank implements Runnable {

    private int times = 0;

    public Vector<EnemyTank> getEnemyTanks() {
        return enemyTanks;
    }

    public Vector<Shot> getShots() {
        return shots;
    }

    public void setShots(Vector<Shot> shots) {
        this.shots = shots;
    }

    //  所有敌人坦克
    private Vector<EnemyTank> enemyTanks = new Vector<>();
    // 所有敌人的子弹
    private Vector<Shot> shots = new Vector<>();

    // 敌人添加子弹，应当在刚刚创建坦克和敌人的坦克子弹死亡后
    public EnemyTank(int x, int y) {
        super(x, y);
    }
// 得到Panel的敌人坦克所有数量

    public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        this.enemyTanks = enemyTanks;
    }

    // 判断是否碰到了别的坦克
    public boolean isTouchOtherEnemyTank() {
        boolean b = false;
        switch (this.direct) {
            case 0:
                // 坦克向上
                // 取出所有的敌人坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this) {// 如果不是自己
                        if (enemyTank.direct == 0 || enemyTank.direct == 2) {// 如果敌人坦克方向是向上或向下
                            //左点
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 20 && this.y >= enemyTank.y && this.y <= enemyTank.y + 30) {
                                return true;
                            }
                            if (this.x + 20 >= enemyTank.x && this.x + 20 <= enemyTank.x+20 && this.y >= enemyTank.y && this.y <= enemyTank.y + 30) {
                                return true;
                            }
                        }

                        if (enemyTank.direct == 3 || enemyTank.direct == 1) {// 如果敌人坦克方向是向左或向右
                            //左点
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 30 && this.y >= enemyTank.y && this.y <= enemyTank.y + 20) {
                                return true;
                            }
                            if (this.x + 20 >= enemyTank.x && this.x + 20 <= enemyTank.x + 30 && this.y >= enemyTank.y && this.y <= enemyTank.y + 20) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 1:
                // 坦克向右
                // 取出所有的敌人坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this) {// 如果不是自己
                        if (enemyTank.direct == 0 || enemyTank.direct == 2) {// 如果敌人坦克方向是向上或向下
                            //左点
                            if (this.x + 30 >= enemyTank.x && this.x + 30 <= enemyTank.x + 20 && this.y >= enemyTank.y && this.y <= enemyTank.y + 30) {
                                return true;
                            }
                            if (this.x + 30 >= enemyTank.x && this.x + 30 <= enemyTank.x + 20 && this.y + 20 >= enemyTank.y && this.y + 20 <= enemyTank.y + 30) {
                                return true;
                            }
                        }

                        if (enemyTank.direct == 3 || enemyTank.direct == 1) {// 如果敌人坦克方向是向左或向右
                            //左点
                            if (this.x + 30 >= enemyTank.x && this.x + 30 <= enemyTank.x + 30 && this.y >= enemyTank.y && this.y <= enemyTank.y + 20) {
                                return true;
                            }
                            if (this.x + 30 >= enemyTank.x && this.x + 30 <= enemyTank.x + 30 && this.y + 20 >= enemyTank.y && this.y + 20 <= enemyTank.y + 20) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 2:
                // 坦克向下
                // 取出所有的敌人坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this) {// 如果不是自己
                        if (enemyTank.direct == 0 || enemyTank.direct == 2) {// 如果敌人坦克方向是向上或向下
                            //左点
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 20 && this.y + 30 >= enemyTank.y && this.y + 30 <= enemyTank.y + 30) {
                                return true;
                            }
                            if (this.x + 20 >= enemyTank.x && this.x + 20 <= enemyTank.x + 20 && this.y + 30 >= enemyTank.y && this.y + 30 <= enemyTank.y + 30) {
                                return true;
                            }
                        }

                        if (enemyTank.direct == 3 || enemyTank.direct == 1) {// 如果敌人坦克方向是向左或向右
                            //左点
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 30 && this.y + 30 >= enemyTank.y && this.y + 30 <= enemyTank.y + 20) {
                                return true;
                            }
                            if (this.x + 20 >= enemyTank.x && this.x + 20 <= enemyTank.x + 30 && this.y + 30 >= enemyTank.y  && this.y + 30 <= enemyTank.y + 20) {
                                return true;
                            }
                        }
                    }
                }
                break;
            case 3:
                // 坦克向左
                // 取出所有的敌人坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    if (enemyTank != this) {// 如果不是自己
                        if (enemyTank.direct == 0 || enemyTank.direct == 2) {// 如果敌人坦克方向是向上或向下
                            //左点
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 20 && this.y >= enemyTank.y && this.y <= enemyTank.y + 30) {
                                return true;
                            }
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 20 && this.y + 20 >= enemyTank.y && this.y + 20 <= enemyTank.y + 30) {
                                return true;
                            }
                        }

                        if (enemyTank.direct == 3 || enemyTank.direct == 1) {// 如果敌人坦克方向是向左或向右
                            //左点
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 30 && this.y >= enemyTank.y && this.y <= enemyTank.y + 20) {
                                return true;
                            }
                            if (this.x >= enemyTank.x && this.x <= enemyTank.x + 30 && this.y + 20 >= enemyTank.y && this.y + 20 <= enemyTank.y + 20) {
                                return true;
                            }
                        }
                    }
                }
                break;
        }
        return b;
    }

    @Override
    public void run() {
        while (true) {
            switch (direct) {
                case 0: // 坦克正在向上运动
                    for (int i = 0; i < 30; i++) {
                        if (y > 0 && !this.isTouchOtherEnemyTank()) {
                            y = y - speed;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 1://向右
                    for (int i = 0; i < 30; i++) {
                        if (x < 400 && !isTouchOtherEnemyTank()) {
                            x = x + speed;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 2: //向下
                    for (int i = 0; i < 30; i++) {
                        if (y < 300 && !isTouchOtherEnemyTank()) {
                            y = y + speed;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                case 3: //向左
                    for (int i = 0; i < 30; i++) {
                        if (x > 0 && !isTouchOtherEnemyTank()) {
                            x = x - speed;
                        }
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
            }
            this.times++;
            if (times % 2 == 0) {
                if (isLive) {
                    if (shots.size() < 5) {
                        Shot shot = null;
                        switch (direct) {
                            case 0: //创建子弹
                                shot = new Shot(x + 10, y, 0);
                                shots.add(shot);
                                break;
                            case 1:
                                shot = new Shot(x + 30, y + 10, 1);
                                shots.add(shot);
                                break;
                            case 2:
                                shot = new Shot(x + 10, y + 30, 2);
                                shots.add(shot);
                                break;
                            case 3:
                                shot = new Shot(x, y + 10, 3);
                                shots.add(shot);
                                break;
                        }
                        Thread thread = new Thread(shot);
                        thread.start();
                    }
                }
            }
            //让坦克随机产生一个新的方向
            this.direct = (int) (Math.random() * 4);
            //判断敌人让坦克是否死亡
            if (this.isLive == false) {
                break;
            }
        }

    }
}
